package com.xiaogao.sys.controller;

import com.xiaogao.common.entity.Result;
import com.xiaogao.common.utils.tree.TreeUtils;
import com.xiaogao.security.annotation.HasPerm;
import com.xiaogao.sys.request.dept.AddParam;
import com.xiaogao.sys.request.dept.UpdateParam;
import com.xiaogao.sys.response.SysDeptResponse;
import com.xiaogao.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService deptService;

    @GetMapping("/query/tree")
    public Result<Object> queryTree(){
        List<SysDeptResponse> sysDeptResponseList = deptService.queryList();
        List<SysDeptResponse> tree = TreeUtils.convertToTree(sysDeptResponseList);
        return Result.success(tree);
    }

    @HasPerm("sys:dept:add")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody @Validated AddParam addParam) {
        deptService.add(addParam);
        return Result.success();
    }

    @HasPerm("sys:dept:update")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody @Validated UpdateParam updateParam) {
        deptService.update(updateParam);
        return Result.success();
    }

    @HasPerm("sys:dept:delete")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable("id") Long id) {
        deptService.delete(id);
        return Result.success();
    }
}
